home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / PEAR.php < prev   
Encoding:
PHP Script  |  2001-03-06  |  9.4 KB  |  376 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net>                          |
  17. // |          Stig Bakken <ssb@fast.no>                                   |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: PEAR.php.in,v 1.9 2000/10/15 09:02:54 ssb Exp $
  21. //
  22.  
  23. define('PEAR_ERROR_RETURN', 1);
  24. define('PEAR_ERROR_PRINT', 2);
  25. define('PEAR_ERROR_TRIGGER', 4);
  26. define('PEAR_ERROR_DIE', 8);
  27. define('PEAR_ERROR_CALLBACK', 16);
  28.  
  29. define('PHP_BINDIR', '/Apache/bin');
  30. define('PEAR_INSTALL_DIR', '/Apache/lib/php');
  31. define('PEAR_EXTENSION_DIR', '/Apache/lib/php/extensions/no-debug-non-zts-20001214');
  32.  
  33. $_PEAR_destructor_object_list = array();
  34.  
  35. //
  36. // Tests needed: - PEAR inheritance
  37. //               - destructors
  38. //
  39.  
  40. /**
  41.  * Base class for other PEAR classes.  Provides rudimentary
  42.  * emulation of destructors.
  43.  *
  44.  * If you want a destructor in your class, inherit PEAR and make a
  45.  * destructor method called _yourclassname (same name as the
  46.  * constructor, but with a "_" prefix).  Also, in your constructor you
  47.  * have to call the PEAR constructor: <code>$this->PEAR();</code>.
  48.  * The destructor method will be called without parameters.  Note that
  49.  * at in some SAPI implementations (such as Apache), any output during
  50.  * the request shutdown (in which destructors are called) seems to be
  51.  * discarded.  If you need to get any debug information from your
  52.  * destructor, use <code>error_log()</code>, <code>syslog()</code> or
  53.  * something like that instead.
  54.  *
  55.  * @since PHP 4.0.2
  56.  * @author Stig Bakken <ssb@fast.no>
  57.  */
  58. class PEAR
  59. {
  60.     // {{{ properties
  61.  
  62.     var $_debug = false;
  63.  
  64.     // }}}
  65.  
  66.     // {{{ constructor
  67.  
  68.     /**
  69.      * Constructor.  Registers this object in
  70.      * $_PEAR_destructor_object_list for destructor emulation.
  71.      */
  72.     function PEAR() {
  73.         global $_PEAR_destructor_object_list;
  74.         $_PEAR_destructor_object_list[] = &$this;
  75.         if ($this->_debug) {
  76.             printf("PEAR constructor called, class=%s\n",
  77.                    get_class($this));
  78.         }
  79.     }
  80.  
  81.     // }}}
  82.     // {{{ destructor
  83.  
  84.     /**
  85.      * Destructor (the emulated type of...).  Does nothing right now,
  86.      * but is included for forward compatibility, so subclass
  87.      * destructors should always call it.
  88.      * 
  89.      * See the note in the class desciption about output from
  90.      * destructors.
  91.      *
  92.      * @access public
  93.      */
  94.     function _PEAR() {
  95.         if ($this->_debug) {
  96.             printf("PEAR destructor called, class=%s\n",
  97.                    get_class($this));
  98.         }
  99.     }
  100.  
  101.     // }}}
  102.     // {{{ isError()
  103.  
  104.     /**
  105.      * Tell whether a value is a PEAR error.
  106.      *
  107.      * @param    $data    the value to test
  108.      * @access    public
  109.      * @return    bool    true if $data is an error
  110.      */
  111.     function isError(&$data) {
  112.         return (bool)(is_object($data) &&
  113.                       (get_class($data) == "pear_error" ||
  114.                        is_subclass_of($data, "pear_error")));
  115.     }
  116.  
  117.     // }}}
  118. }
  119.  
  120. // {{{ _PEAR_call_destructors()
  121.  
  122. function _PEAR_call_destructors() {
  123.     global $_PEAR_destructor_object_list;
  124.     if (is_array($_PEAR_destructor_object_list) && sizeof($_PEAR_destructor_object_list)) {
  125.     reset($_PEAR_destructor_object_list);
  126.     while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  127.         $destructor = "_".get_class($objref);
  128.         if (method_exists($objref, $destructor)) {
  129.         $objref->$destructor();
  130.         }
  131.     }
  132.     // Empty the object list to ensure that destructors are
  133.     // not called more than once.
  134.     $_PEAR_destructor_object_list = array();
  135.     }
  136. }
  137.  
  138. // }}}
  139.  
  140. class PEAR_Error
  141. {
  142.     // {{{ properties
  143.  
  144.     var $error_message_prefix = '';
  145.     var $error_prepend        = '';
  146.     var $error_append         = '';
  147.     var $mode                 = PEAR_ERROR_RETURN;
  148.     var $level                = E_USER_NOTICE;
  149.     var $code                 = -1;
  150.     var $message              = '';
  151.     var $debuginfo            = '';
  152.  
  153.     // Wait until we have a stack-groping function in PHP.
  154.     //var $file    = '';
  155.     //var $line    = 0;
  156.     
  157.  
  158.     // }}}
  159.     // {{{ constructor
  160.  
  161.     /**
  162.      * PEAR_Error constructor
  163.      *
  164.      * @param $message error message
  165.      *
  166.      * @param $code (optional) error code
  167.      *
  168.      * @param $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  169.      * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER or
  170.      * PEAR_ERROR_CALLBACK
  171.      *
  172.      * @param $level (optional) error level, _OR_ in the case of
  173.      * PEAR_ERROR_CALLBACK, the callback function or object/method
  174.      * tuple.
  175.      *
  176.      * @access public
  177.      *
  178.      */
  179.     function PEAR_Error($message = 'unknown error',
  180.                         $code = 0,
  181.                         $mode = null,
  182.                         $options = null,
  183.                         $debuginfo = null)
  184.     {
  185.         if ($mode === null) {
  186.             $mode = PEAR_ERROR_RETURN;
  187.         }
  188.         $this->message   = $message;
  189.         $this->code      = $code;
  190.         $this->mode      = $mode;
  191.         $this->debuginfo = $debuginfo;
  192.         if ($mode & PEAR_ERROR_CALLBACK) {
  193.             $this->level = E_USER_NOTICE;
  194.             $this->callback = $options;
  195.         } else {
  196.             if ($options === null) {
  197.                 $options = E_USER_NOTICE;
  198.             }
  199.             $this->level = $options;
  200.             $this->callback = null;
  201.         }
  202.         if ($this->mode & PEAR_ERROR_PRINT) {
  203.             print $this->getMessage();
  204.         }
  205.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  206.             trigger_error($this->getMessage(), $this->level);
  207.         }
  208.         if ($this->mode & PEAR_ERROR_DIE) {
  209.             die($this->getMessage());
  210.         }
  211.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  212.             if (is_string($this->callback) && strlen($this->callback)) {
  213.                 call_user_func($this->callback, $this);
  214.             } elseif (is_array($this->callback) &&
  215.                       sizeof($this->callback) == 2 &&
  216.                       is_object($this->callback[0]) &&
  217.                       is_string($this->callback[1]) &&
  218.                       strlen($this->callback[1])) {
  219.                 call_user_method($this->callback[1], $this->callback[0],
  220.                                  $this);
  221.             }
  222.         }
  223.     }
  224.  
  225.     // }}}
  226.     // {{{ getMode()
  227.  
  228.     /**
  229.      * Get the error mode from an error object.
  230.      *
  231.      * @return int error mode
  232.      * @access public
  233.      */
  234.     function getMode() {
  235.         return $this->mode;
  236.     }
  237.  
  238.     // }}}
  239.     // {{{ getCallback()
  240.  
  241.     /**
  242.      * Get the callback function/method from an error object.
  243.      *
  244.      * @return mixed callback function or object/method array
  245.      * @access public
  246.      */
  247.     function getCallback() {
  248.         return $this->callback;
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ getMessage()
  253.  
  254.     
  255.     /**
  256.      * Get the error message from an error object.
  257.      *
  258.      * @return    string    full error message
  259.      * @access public
  260.      */
  261.     function getMessage ()
  262.     {
  263.         return ($this->error_prepend . $this->error_message_prefix .
  264.                 $this->message       . $this->error_append);
  265.     }
  266.     
  267.  
  268.     // }}}
  269.     // {{{ getCode()
  270.     
  271.     /**
  272.      * Get error code from an error object
  273.      *
  274.      * @return int error code
  275.      * @access public
  276.      */
  277.      function getCode()
  278.      {
  279.          return $this->code;
  280.      }
  281.  
  282.     // }}}
  283.     // {{{ getType()
  284.  
  285.     /**
  286.      * Get the name of this error/exception.
  287.      *
  288.      * @return string error/exception name (type)
  289.      * @access public
  290.      */
  291.     function getType ()
  292.     {
  293.         return get_class($this);
  294.     }
  295.  
  296.     // }}}
  297.     // {{{ getDebugInfo()
  298.  
  299.     /**
  300.      * Get additional debug information supplied by the application.
  301.      *
  302.      * @return string debug information
  303.      * @access public
  304.      */
  305.     function getDebugInfo ()
  306.     {
  307.         return $this->debuginfo;
  308.     }
  309.  
  310.     // }}}
  311.     // {{{ toString()
  312.  
  313.     /**
  314.      * Make a string representation of this object.
  315.      *
  316.      * @return string a string with an object summary
  317.      * @access public
  318.      */
  319.     function toString() {
  320.         $modes = array();
  321.         $levels = array(E_USER_NOTICE => "notice",
  322.                         E_USER_WARNING => "warning",
  323.                         E_USER_ERROR => "error");
  324.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  325.             if (is_array($this->callback)) {
  326.                 $callback = get_class($this->callback[0]) . "::" .
  327.                     $this->callback[1];
  328.             } else {
  329.                 $callback = $this->callback;
  330.             }
  331.             return sprintf('[%s: message="%s" code=%d mode=callback '.
  332.                            'callback=%s prefix="%s" prepend="%s" append="%s" '.
  333.                            'debug="%s"]',
  334.                            get_class($this), $this->message, $this->code,
  335.                            $callback, $this->error_message_prefix,
  336.                            $this->error_prepend, $this->error_append,
  337.                            $this->debuginfo);
  338.         }
  339.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  340.             $modes[] = "callback";
  341.         }
  342.         if ($this->mode & PEAR_ERROR_PRINT) {
  343.             $modes[] = "print";
  344.         }
  345.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  346.             $modes[] = "trigger";
  347.         }
  348.         if ($this->mode & PEAR_ERROR_DIE) {
  349.             $modes[] = "die";
  350.         }
  351.         if ($this->mode & PEAR_ERROR_RETURN) {
  352.             $modes[] = "return";
  353.         }
  354.         return sprintf('[%s: message="%s" code=%d mode=%s level=%s prefix="%s" '.
  355.                        'prepend="%s" append="%s" debug="%s"]',
  356.                        get_class($this), $this->message, $this->code,
  357.                        implode("|", $modes), $levels[$this->level],
  358.                        $this->error_message_prefix,
  359.                        $this->error_prepend, $this->error_append,
  360.                        $this->debuginfo);
  361.     }
  362.  
  363.     // }}}
  364. }
  365.  
  366. register_shutdown_function("_PEAR_call_destructors");
  367.  
  368. /*
  369.  * Local Variables:
  370.  * mode: c++
  371.  * tab-width: 4
  372.  * c-basic-offset: 4
  373.  * End:
  374.  */
  375. ?>
  376.